﻿

#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;
using DTO;
#endregion

namespace DAO
{
    public class KhachHangDAO : Dataprovider
    {
        #region Phuong thuc
        public KhachHangDAO()
        {
        }

        // lay danh sach khach hang.
        public ArrayList getDsKhachHang()
        {
            connect();
            string query = "SELECT * FROM KHACHHANG";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);

            return arr;
        }

        // lay du lieu dua vao dataRow
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            KhachHangDTO kh = new KhachHangDTO();

            kh.MaKhachHang = dt.Rows[i]["MaKhachHang"].ToString();
            kh.TenKhachHang = dt.Rows[i]["TenKhachHang"].ToString();
            kh.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            kh.SoDienThoai = dt.Rows[i]["SoDienThoai"].ToString();
            
            return (object)kh;
        }

        public void insert(KhachHangDTO info)
        {
            string insertCommand = "INSERT INTO KHACHHANG VALUES('" +
                info.MaKhachHang + "', '" +
                info.TenKhachHang + "', '" +
                info.DiaChi + "', '" +
                info.SoDienThoai + "')";

            executeNonQuery(insertCommand);
        }

        public void update(KhachHangDTO info)
        {
            string updateCommand = "UPDATE KHACHHANG " +
                                    "SET TenKhachHang = '" + info.TenKhachHang + "', " +
                                    " DiaChi = '" + info.DiaChi + "', " +
                                    " SoDienThoai = '" + info.SoDienThoai + "', " +
                                    " WHERE MaKhachHang = '" + info.MaKhachHang + "'";


            executeNonQuery(updateCommand);
        }

        public void delete(string maKH)
        {
            string deleteCommand = "DELETE FROM KHACHHANG WHERE MaKhachHang = '" + maKH + "'";
            executeNonQuery(deleteCommand);
        }
        #endregion
    }
}
